/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package graphic;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import libPackage.ConstHelper;

//import sma.combatgame.agents.map.MapAgent;
//import sma.combatgame.utils.Utils;
public class World extends JFrame {

    private JPanel[][] grid;

    private HashMap<String, Point> agents;

    private ArrayList<JLabel> jLabels;
    private int WIDTH = 40;
    private int HEIGHT = 40;

    public World() {
        jLabels = new ArrayList<>();
        initGrid();
    }

    public void setAgents(HashMap<String, Point> agents) {
        this.agents = new HashMap<>(agents);
    }
    
    private void initGrid() {
        JPanel field = new JPanel(new GridLayout(WIDTH, HEIGHT));

        Border blackline = BorderFactory.createLineBorder(Color.black, 1);
        grid = new JPanel[WIDTH][HEIGHT];

        Dimension cellSize = new Dimension(10, 10);
        for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                grid[i][j] = new JPanel();
                grid[i][j].setPreferredSize(cellSize);
                //grid[i][j].setBorder(blackline);
                //grid[i][j].setBackground(new Color(25, 204, 89));

                field.add(grid[i][j]);
            }
        }
        field.setBackground(new Color(25, 204, 89));
        field.setBorder(blackline);
        Container display = getContentPane();
        display.setBackground(Color.GREEN);
        display.setLayout(new BorderLayout());
        display.add(field);
    }

    public void addAgentsToWorld() {
        for (Entry<String, Point> e : agents.entrySet()) {
            int agentType = getAgentType(e.getKey());
            JLabel label;
            switch (agentType) {
                case ConstHelper.AGENT_TYPE_CARNIVORE:
                    label = new JLabel(new ImageIcon("res/wolf.png"));
                    jLabels.add(label);
                    grid[e.getValue().x][e.getValue().y].add(label);
                    break;
                case ConstHelper.AGENT_TYPE_HERBIVORE:
                    label = new JLabel(new ImageIcon("res/sheep.png"));
                    jLabels.add(label);
                    grid[e.getValue().x][e.getValue().y].add(label);
                    break;
                case ConstHelper.AGENT_TYPE_HOMME:
                    label = new JLabel(new ImageIcon("res/person.png"));
                    jLabels.add(label);
                    grid[e.getValue().x][e.getValue().y].add(label);
                    break;
                case ConstHelper.AGENT_TYPE_PLANTE:
                    label = new JLabel(new ImageIcon("res/grass.png"));
                    jLabels.add(label);
                    grid[e.getValue().x][e.getValue().y].add(label);
                    break;
                default:
                    break;
            }
        }
    }

    private void clearAllAgents() {
        if (jLabels.isEmpty()) {
            return;
        }
        for (JLabel jLabel : jLabels) {
            Container parent = jLabel.getParent();
            parent.remove(jLabel);
            parent.validate();            
        }
        repaint();
        jLabels.clear();
    }

//        private void clearAgents(int number, JLabel[] myArray) {
//        for (int i = 0; i < number; i++) {
//            Container parent = myArray[i].getParent();
//            parent.remove(myArray[i]);
//            parent.validate();
//        }
//        flag = 1;
//    }
    public void showGui(HashMap<String, Point> agentList) {
        if (!agentList.isEmpty()) {
            clearAllAgents();
            setAgents(agentList);
            addAgentsToWorld();
            System.out.println(agentList.size());
        }

        pack();
        this.setTitle("Le monde");

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int centerX = (int) screenSize.getWidth() / 2;
        int centerY = (int) screenSize.getHeight() / 2;
        setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);

        super.setVisible(true);
    }

//    public static void main(String[] args) throws InterruptedException {
//        World ai = new World();
//        HashMap<String, Point> agentList = new HashMap<>();
//        Random rdn = new Random();
//        agentList.put("homme_1", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("homme_2", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("plante_3", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("plante_4", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("plante_5", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("plante_6", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("plante_7", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("plante_8", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("plante_9", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("plante_10", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("plante_11", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("carnivore_4", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("herbivore_5", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("plante_1", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        agentList.put("homme_7", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//        ai.showGui(agentList);
//        for (int i = 0; i < 10; i++) {
//            Thread.sleep(1000);
//            agentList.put("homme_1", new Point(rdn.nextInt(30), rdn.nextInt(20)));
//            ai.showGui(agentList);
//
//        }
//
//    }

    private int getAgentType(String key) {
        String[] agentType = key.split("_");
        if (agentType[0].equals("carnivore")) {
            return ConstHelper.AGENT_TYPE_CARNIVORE;
        }
        if (agentType[0].equals("herbivore")) {
            return ConstHelper.AGENT_TYPE_HERBIVORE;
        }
        if (agentType[0].equals("homme")) {
            return ConstHelper.AGENT_TYPE_HOMME;
        }
        if (agentType[0].equals("plante")) {
            return ConstHelper.AGENT_TYPE_PLANTE;
        }
        return -1;
    }

}
